﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WebEntities;
using WebEntities.Models.Framework;

namespace com.subb.WebFramework
{
    public class FW_MemGroupDAL
    {
        public List<FW_MemGroup> GetAllMemGroups(int start, int limit, out int ttlCount)
        {
            ttlCount = 0;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                var query = ctx.FW_MemGroup.OrderBy(c => c.GroupID);
                ttlCount = query.Count();
                List<FW_MemGroup> grplist = query.Skip(start).Take(limit).ToList();
                return grplist;
            }
        }

        public FW_MemGroup GetMemGroup(int grpId)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                ctx.ContextOptions.LazyLoadingEnabled = false;
                FW_MemGroup grp = ctx.FW_MemGroup.SingleOrDefault(c => c.GroupID == grpId);
                return grp;
            }
        }

        public void CreateMemGroup(MemGroup v_memgrp)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                FW_MemGroup memgrp = new FW_MemGroup
                {
                    GroupName = v_memgrp.GroupName,
                    GroupType = v_memgrp.GroupType,
                    CreateTime = DateTime.Now,
                    Creator = v_memgrp.Creator
                };
                ctx.FW_MemGroup.AddObject(memgrp);
                ctx.SaveChanges();
            }
        }

        public void RemoveMemGroup(int grpId)
        {
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                FW_MemGroup memgrp = ctx.FW_MemGroup.SingleOrDefault(c => c.GroupID == grpId);
                if (memgrp != null)
                {
                    ctx.FW_MemGroup.DeleteObject(memgrp);
                    ctx.SaveChanges();
                }
            }
        }

        public bool CheckExistGroupName(string grpName)
        {
            bool result = false;
            using (WebsiteDbEntities ctx = new WebsiteDbEntities())
            {
                int count = ctx.FW_MemGroup.Where(c => c.GroupName.Equals(grpName, StringComparison.CurrentCultureIgnoreCase)).Count();
                result = count > 0 ? true : false;
            }
            return result;
        }
    }
}
